package com.wswy.wzcx.ui.car.choose

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import com.che.libcommon.utils.RxHelper
import com.che.libcommon.utils.optional.Optional
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.model.WZCity
import com.wswy.wzcx.model.car.JZGCity
import com.wswy.wzcx.model.resp.ProvinceData
import com.wswy.wzcx.module.base.BaseViewModel
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.module.base.onResult
import com.wswy.wzcx.utils.RxUtils
import java.util.*
import kotlin.collections.ArrayList

class ChooseCityViewModel : BaseViewModel() {

    private val jzgCityLiveData = MutableLiveData<Resource<Map<String,List<JZGCity>>>>()

    private val wzCityLiveData = MutableLiveData<Resource<Map<String,MutableList<WZCity>>>>()

    private val provCityLiveData = MutableLiveData<Resource<Map<String,MutableList<ProvinceData>>>>()


    fun loadJZGCity(){

        addRun(Api.get().carTypeJZGCity().onResult(jzgCityLiveData))

    }

    fun loadWZCity(){
        val map = Api.get().cityList().compose(RxUtils.switch2io()).map {

            val ret  = mutableMapOf<String,MutableList<WZCity>>()

            it.orNull()?.run {

                directCityList?.forEach {

                    ret.getOrPut(it.nameLatter){
                        mutableListOf()
                    }.add(it)

                }

                cityList?.forEach {province ->

                    province.list?.forEach {
                        it.pid = province.pid
                        it.pname = province.name
                        ret.getOrPut(it.nameLatter){
                            mutableListOf()
                        }.add(it)
                    }

                }

            }

            return@map Optional.of(ret.toMap())
        }
        addRun(map.onResult(wzCityLiveData))
    }


    fun loadWZProvCity(){
        val map = Api.get().cityList().compose(RxUtils.switch2io()).map {
            val ret  = mutableMapOf<String,MutableList<ProvinceData>>()

            it.orNull()?.run {
                //直辖市
                directCityList?.forEach {city ->

                    ret.getOrPut(city.nameLatter){
                        mutableListOf()
                    }.add(ProvinceData().apply {

                        name = city.name
                        nameCn = city.nameCn
                        pid = city.pid

                        list = listOf(city)

                    })

                }
                //省市
                cityList?.forEach {province ->

                    val outList = ArrayList<WZCity>()
                    //保存省份
                    province?.list?.forEach {
                        it.pid = province.pid
                        it.pname = province.name

                        outList.add(it)
                    }
                    //避免和缓存冲突，并发错误
                    outList.sortWith(Comparator { t1, t2 ->
                        t1.areaCode.compareTo(t2.areaCode)
                    })

                    province?.list = outList

                    ret.getOrPut(province.nameLatter){
                        mutableListOf()
                    }.add(province)
                }

            }

            return@map Optional.of(ret.toMap())
        }
        addRun(map.onResult(provCityLiveData))
    }


    fun getJZGCityLiveData():LiveData<Resource<Map<String,List<JZGCity>>>> = jzgCityLiveData


    fun getWZCityLiveData():LiveData<Resource<Map<String,MutableList<WZCity>>>> = wzCityLiveData

    fun getProvCityLiveData():LiveData<Resource<Map<String,MutableList<ProvinceData>>>> = provCityLiveData

}